package com.wh.testCallable;

import com.wh.test.Test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 1:怎么才能通过new Thread().start()执行Callable
 *   Runnable----(实现类)--->FutureTask------>FutureTask(Callable)
 *
 * Callable细节
 * 1：有缓存
 * 2:结果可能需要等待，会阻塞
 */
public class TCallable {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //怎么启动Callable?
        //new Thread(new Runnable()).start()
        //new Thread(new FutureTask<V>).start()
        //new Thread(new FutureTask<V>(Callable)).start()
        TestCallable testCallable = new TestCallable();
        FutureTask<Integer> futureTask = new FutureTask<>(testCallable);//适配类
        new Thread(futureTask).start();
        new Thread(futureTask).start();//结果会被缓存，效率高
        Integer result = futureTask.get();//获取Callable的返回结果 可能会产生阻塞<--等待结果返回  一般放在最后或者使用异步通讯
        System.out.println(result);
    }
}
class TestCallable implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        System.out.println("call()");//当有有个线程调用，会打印几个call
        return 123;
    }
}